<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 词语过滤模型
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class BadwordModel extends CommonModel{

    /**
     * 返回对象的定义
     */
    static public function init__(){

        return array(

            'table_name' => 'badword',

            'check' => array(

                'badword_find'=>array(

                    array( 'require',G::L( '待过滤的词语不能为空!' ) ),
                ),
            ),

	        'autofill' => array(

    	        array( 'badword_findpattern','findPattern','ALL','callback'),
    	        array( 'badword_admin','admin','create','callback'),
             ),
        );
    }


    /**
     * 开启一个查询，查找符合条件的对象或对象集合
     */
    static function F(){

        $arrArgs = func_get_args();
        return ModelMeta::instance( __CLASS__ )->findByArgs( $arrArgs );
    }

    /**
     * 返回当前 Model 类的元数据对象
     */
    static function M(){

        return ModelMeta::instance( __CLASS__ );
    }

    /**
     * 清空badword表的数据
     */
    public function truncateBadword( ) {

    	$oDb = $this->getDb();
		return $oDb->query( "TRUNCATE ".G::C('DB_PREFIX')."badword" );
	}

	/**
	 * 添加过滤词语
	 */
	public function addBadword( $sFind, $sReplacement, $sAdmin, $nType=1) {

		if( trim( $sFind ) ) {

			$sFind = trim( $sFind );
			$sReplacement = trim( $sReplacement );
			$sFindpattern = $this->patternFind( $sFind );

			$arrBadwordData = array(

			    'badword_find'=>$sFind ,
			    'badword_replacement'=>$sReplacement,
			    'badword_admin'=>$sAdmin,
			    'badword_findpattern'=>$sFindpattern,
			);

			$oBadword = new self( $arrBadwordData );

			// 拒绝自动填充，防止数据覆盖
			$oBadword->setAutofill( false );

			if( $nType == 1) {

				$oBadword->save( 'replace' );
			}

			 elseif( $nType == 2) {

				$oBadword->save( );
			}

			// 捕获错误
			if( $oBadword->isError() ){

                $this->setErrorMessage( $oBadword->getErrorMessage() );
				return false;
			}

		}

		return $oBadword->getDb()->getConnect()->getInsertId();
	}

    /**
     * badword_findpattern 字段回调处理
     */
    protected function findPattern(){

    	return $this->patternFind( Safe::getGpc( 'badword_find','P' ) );
    }

    /**
     * badword_admin 字段填充回调
     */
    protected function admin(){

    	$arrUserData = UserModel::M()->userData();
    	return $arrUserData['user_name'];
    }

	/**
	 * badword 过滤正则生成
	 */
    protected function patternFind( $sFind ) {

		$sFind = preg_quote( $sFind, "/'" );
		$sFind = str_replace("\\", "\\\\", $sFind );
		$sFind = str_replace("'", "\\'", $sFind );
		return '/'.preg_replace("/\\\{(\d+)\\\}/", ".{0,\\1}", $sFind ).'/is';
	}

}
